//
//  HWCashWithdrawalView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/15.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWCashWithdrawalDetailView.h"
#import "HWAiPayWithdrawView.h"

@interface HWCashWithdrawalDetailView()<UITextFieldDelegate>
@property (nonatomic, strong)UILabel *moneyLabel;//余额
@property (nonatomic, strong)UITextField *moneyTextField;//余额提现

@property (nonatomic, strong)NSString *money;//金额
@property (nonatomic, strong)NSString *cashWithdrawalStr;//余额

@property (nonatomic, strong)UIButton *cancelButton;//取消
@property (nonatomic, strong)HWCashWidthDrawModel *model;
@end

@implementation HWCashWithdrawalDetailView

- (UILabel *)moneyLabel
{
    if (!_moneyLabel)
    {
        _moneyLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,19*kUIScaleBasePlus,SCREEN_WIDTH,16*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentCenter
                                             textClor:LCHexColor(0x000000)
                                             textFont:15*kUIScaleBasePlus];
    }
    return _moneyLabel;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton)
    {
        _cancelButton = [HWUtilties creatButtonWithFrame:CGRectMake(324*kUIScaleBasePlus,103*kUIScaleBasePlus,13*kUIScaleBasePlus,13*kUIScaleBasePlus)
                                            textAligment:NSTextAlignmentLeft
                                                    text:@""
                                                textClor:nil
                                               buttonBGC:[UIColor clearColor]
                                                textFont:0
                                                  target:self
                                                  method:@selector(clearMoney)];
        [_cancelButton setImage:[UIImage imageNamed:@"cacelx"] forState:UIControlStateNormal];
        _cancelButton.hidden = YES;
    }
    return _cancelButton;
}

- (UITextField *)moneyTextField
{
    if (!_moneyTextField)
    {
        UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(19*kUIScaleBasePlus,58*kUIScaleBasePlus,80*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentLeft
                                                     textClor:LCHexColor(0x828282)
                                                     textFont:12*kUIScaleBasePlus];
        titleLabel.text = @"自定义兑换";
        [self addSubview:titleLabel];
        
        _moneyTextField = [[UITextField alloc]initWithFrame:CGRectMake(19*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+10*kUIScaleBasePlus,337*kUIScaleBasePlus,62*kUIScaleBasePlus)];
        _moneyTextField.placeholder = @"请输入兑换金额";
        _moneyTextField.delegate = self;
        _moneyTextField.textAlignment = NSTextAlignmentCenter;
        _moneyTextField.layer.masksToBounds = YES;
        _moneyTextField.layer.cornerRadius = 4;
        _moneyTextField.layer.borderColor = LCHexColor(0xF54337).CGColor;
        _moneyTextField.layer.borderWidth = 1;
        _moneyTextField.textColor = LCHexColor(0xF54337);
        _moneyTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_moneyTextField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        
        UIButton *sureBtn = [HWUtilties creatButtonWithFrame:CGRectMake(19*kUIScaleBasePlus,CGRectGetMaxY(_moneyTextField.frame)+30*kUIScaleBasePlus,337*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"确认兑换"
                                                    textClor:LCHexColor(0xFFFFFF)
                                                   buttonBGC:LCHexColor(0xF54337)
                                                    textFont:18*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(sureBtnClick)];
        sureBtn.layer.masksToBounds = YES;
        sureBtn.layer.cornerRadius = 4;
        [self addSubview:sureBtn];
        
        
        UIButton *setAiPayBtn = [HWUtilties creatButtonWithFrame:CGRectMake(19*kUIScaleBasePlus,CGRectGetMaxY(sureBtn.frame)+30*kUIScaleBasePlus,337*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"设置提现支付宝账号"
                                                    textClor:LCHexColor(0xFFFFFF)
                                                   buttonBGC:LCHexColor(0xF54337)
                                                    textFont:18*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(setAiPayClick)];
        setAiPayBtn.layer.masksToBounds = YES;
        setAiPayBtn.layer.cornerRadius = 4;
        [self addSubview:setAiPayBtn];
    }
    return _moneyTextField;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.moneyLabel];
        [self addSubview:self.moneyTextField];
        [self addSubview:self.cancelButton];
        [self attention];
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
        
        //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
        
        tapGestureRecognizer.cancelsTouchesInView = NO;
        
        //将触摸事件添加到view上view可以换成任意一个控件的
        
        [self addGestureRecognizer:tapGestureRecognizer];
    }
    return self;
}

#pragma mark - 温馨提示
- (void)attention
{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16*kUIScaleBasePlus,296*kUIScaleBasePlus,3*kUIScaleBasePlus,15*kUIScaleBasePlus)];
    lineView.backgroundColor = LCHexColor(0xF54337);
    [self addSubview:lineView];
    
    UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame)+10*kUIScaleBasePlus,296*kUIScaleBasePlus, 120*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentLeft
                                                 textClor:LCHexColor(0x333333)
                                                 textFont:15*kUIScaleBasePlus];
    titleLabel.text = @"温馨提示";
    [titleLabel setFont:[UIFont boldSystemFontOfSize:15*kUIScaleBasePlus]];
    [self addSubview:titleLabel];
    
    UILabel *contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(16*kUIScaleBasePlus,CGRectGetMaxY(titleLabel.frame)+19*kUIScaleBasePlus,345*kUIScaleBasePlus,91*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x595656)
                                                   textFont:13*kUIScaleBasePlus];
    contextLabel.numberOfLines = 0;
    contextLabel.text = @"1.提现申请将在1个工作日内完成审批，请耐心等待（双休日及法定节假日除外）\n2.提现到账查询：支付宝-我的-钱包。如有问题，可关注公众号【嗨星球】咨询\n3.提现周期为一个自然年，每年的1月1日00:00将清空未提现的余额，请在年底前完成提现\n4.申请提交后，工作人员会对申请进行审核，若确认用户是以刷单的方式来获取余额，则会归入非常规用户一列，并会对其账号进行冻结操作。";
    [contextLabel sizeToFit];
    [self addSubview:contextLabel];
}

#pragma mark - method
- (void)sureBtnClick
{
    
    
    
    if ([self.money floatValue] > [self.cashWithdrawalStr floatValue])
    {
        [HWUtilties showAlertWithTitle:@"提示"
                               content:@"超出当前余额"
                           buttonTitle:@"确定"
                      ConfirmWithBlock:^{
                          
                      }];
        
        return;
    }
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"withdrawAmount"   : self.cashWithdrawalStr
                               };
    
    WeakSelf(self)
    [MyInfoManager setAiPayCount:prarmers sucess:^(id  _Nonnull acceptData) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showSuccessWithStatus:@"提现成功"];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"设置支付宝账号失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

- (void)setAiPayClick
{
    HWAiPayWithdrawView *view = [[HWAiPayWithdrawView alloc]initWithFrame:self.bounds];
    [self buryPoint];
    [view getDate:self.model];
    [self addSubview:view];
}

- (void)textChange:(UITextField *)textfield
{
    self.money = textfield.text;
    
    if (self.cancelButton.hidden)
    {
        self.cancelButton.hidden = NO;
    }
    
    if ([textfield.text isEqualToString:@""])
    {
        self.cancelButton.hidden = YES;
    }
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap
{
    
    [self.moneyTextField resignFirstResponder];//textFiled是指您声明的UITextFiled控件的
    
}

- (void)clearMoney
{
    self.cancelButton.hidden = YES;
    self.moneyTextField.text = @"";
}
#pragma mark - textField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}



#pragma mark - date
- (void)getDate:(HWCashWidthDrawModel *)model;
{
    self.model = model;
    NSString *money = [NSString stringWithFormat:@"当前余额：%@",[model.canWithdrawCash stringValue]];
    
    self.moneyLabel.attributedText = [money addAttriButedString:money
                                                          Color:LCHexColor(0xF54337)
                                                           Font:[UIFont systemFontOfSize:18*kUIScaleBasePlus]
                                                           rang:NSMakeRange(5,money.length-5)];
    self.cashWithdrawalStr = [money substringWithRange:NSMakeRange(5,money.length-6)];
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:nil
                               fromId:@"606b94b727fef305"
                                 toId:nil];
}
@end
